package com.liuzhongni.wahahanews.activity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;

import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.dao.DaoMaster;
import com.liuzhongni.wahahanews.dao.DaoSession;
import com.liuzhongni.wahahanews.dao.Person;
import com.liuzhongni.wahahanews.dao.PersonDao;
import com.liuzhongni.wahahanews.fragment.MyselfFragment;
import com.liuzhongni.wahahanews.fragment.NewsFragment;
import com.liuzhongni.wahahanews.fragment.ReadFragment;
import com.liuzhongni.wahahanews.fragment.TopicFragment;
import com.liuzhongni.wahahanews.fragment.VideoFragment;

import java.util.ArrayList;

import cn.jpush.android.api.JPushInterface;
import de.greenrobot.dao.query.QueryBuilder;


public class MainActivity extends FragmentActivity {
    private TabHost tabHost;

    private android.support.v4.app.FragmentManager fm;
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    public static DaoSession daoSession;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"wahaha",null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();

        daoSession.getPersonDao().insert(new Person());

        QueryBuilder qb = daoSession.getPersonDao().queryBuilder();
        qb.where(PersonDao.Properties.Name.eq("邹灰灰"));
        ArrayList<Person> data = (ArrayList<Person>) qb.list();
        for (int i = 0; i <data.size(); i++) {
            Log.d("MainActivity", "data.size():" + data.get(i).getName());
        }
//        daoSession.getDogDao().insert(new Dog(1l,"邹辉"));
//        Log.d("MainActivity",daoSession.getPersonDao().queryRaw("name", "邹灰灰").size()+"");
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
        fm = getSupportFragmentManager();
        int[] tabLayout = {R.layout.news_tab, R.layout.read_tab, R.layout.video_tab, R.layout.topic_tab, R.layout.myself_tab};
        int[] tabViewId = {R.id.news, R.id.read, R.id.video, R.id.topic, R.id.myself};
        for (int i = 0; i < tabLayout.length; i++) {
            View view = getLayoutInflater().inflate(tabLayout[i], null);
            tabHost.addTab(tabHost.newTabSpec("").setIndicator(view).setContent(tabViewId[i]));
        }
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.news, new NewsFragment());
        transaction.replace(R.id.read, new ReadFragment());
        transaction.replace(R.id.video, new VideoFragment());
        transaction.replace(R.id.topic, new TopicFragment());
        transaction.replace(R.id.myself, new MyselfFragment());
        transaction.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }


}
